package com.pj.wisdomlogistics.ui.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.pj.spinnerlib.views.DensityUtil;
import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.CarPoritionInfo;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.DriverChooseAdapter;
import com.pj.wisdomlogistics.ui.view.GridSpacingItemDecoration;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;


public class DriverChooseFragment extends BaseFragment implements DriverChooseAdapter.listener {
    @BindView(R.id.rv_choose_driver)
    RecyclerView mRvChooseDriver;

    private DriverChooseAdapter mAdapter;
    private List<CarPoritionInfo.DriverListBean> mList;
    private int mSelectedPos = -1;

    @Override
    protected void initView() {
        mRvChooseDriver.setLayoutManager(new GridLayoutManager(getContext() , 2 , LinearLayoutManager.VERTICAL , false));
        mRvChooseDriver.addItemDecoration(new GridSpacingItemDecoration(2 , DensityUtil.dip2px(getContext() , 20) , false));
        mAdapter = new DriverChooseAdapter(getContext());
        mRvChooseDriver.setAdapter(mAdapter);
        mAdapter.setListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public int getContentView() {
        return R.layout.fragment_choose_driver;
    }

    @Override
    protected boolean isApplyEventBus() {
        return true;
    }

    @Override
    protected boolean isApplyButterKnife() {
        return true;
    }

    @Override
    protected void onEventCome(EventCenter eventCenter) {
        if(eventCenter.getEventCode() == Cons.CarInfo.DRIVER_CHOOSE){
            mList = (List<CarPoritionInfo.DriverListBean>) eventCenter.getData();
            mAdapter.setList(mList);
        }
    }

    @Override
    public void itemClick(int position) {
        EventBus.getDefault().post(new EventCenter<>(Cons.CarInfo.DRIVER_CHOOSE_OK , position));
        BaseViewHolder vh;
        if(mSelectedPos == -1){
            vh = (BaseViewHolder) mRvChooseDriver.findViewHolderForLayoutPosition(position);
        }else {
            vh = (BaseViewHolder) mRvChooseDriver.findViewHolderForLayoutPosition(mSelectedPos);
        }

        if(vh != null){
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_blue);
        }else{
            mAdapter.notifyItemChanged(mSelectedPos);
        }

        if(mSelectedPos == -1){
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
        }else{
            mList.get(mSelectedPos).setSelected(false);
            mSelectedPos = position;
            mList.get(mSelectedPos).setSelected(true);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.portion_bg_normal);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_white);
            vh = (BaseViewHolder) mRvChooseDriver.findViewHolderForLayoutPosition(mSelectedPos);
            vh.getView(R.id.ll_layout).setBackgroundResource(R.drawable.select);
            vh.getImageView(R.id.iv_icon).setImageResource(R.drawable.people_blue);
        }
    }

}
